package com.transfiguro.visitors;

import com.transfiguro.datamodel.javamodels.Imports;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.ImportDeclaration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class will get all imports for a given class
 */
public class ImportsVisitor {
    private final Logger logger = LoggerFactory.getLogger(ImportsVisitor.class);
    private Imports imports;

    /**
     * Constuructor
     */
    public ImportsVisitor() {
        imports = new Imports();
    }

    /**
     * Builds the {@link Imports} instance by getting all the imports from the
     * given {@link CompilationUnit} instance
     *
     * @param unit The <code>CompilationUnit</code> from which imports have to be extracted
     * @return The <code>Imports</code>
     */
    public Imports getImports(final CompilationUnit unit) {
        if (unit.getImports() != null) {
            for (final ImportDeclaration importDeclaration : unit.getImports()) {
                imports.addImport(importDeclaration.getName().toString());
            }
        }
        return imports;
    }

}
